package com.nineclock.message.service.impl;

import cn.hutool.core.util.StrUtil;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.message.dto.NcMessageDto;
import com.nineclock.message.dto.NcMessageQueryDTO;
import com.nineclock.message.enums.MessageTypeEnum;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.NcMessageService;
import com.nineclock.message.utils.JPushManager;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NcMessageServiceImpl implements NcMessageService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private JPushManager pushManager;

    @Override
    public void pushMessage(NcMessage ncMessage) {
        //封装NcMessage属性
        String title = MessageTypeEnum.getEnumByType(ncMessage.getMessageType()).getTitle();
        String audience = MessageTypeEnum.getEnumByType(ncMessage.getMessageType()).getAudience();

        ncMessage.setTitle(title);
        ncMessage.setAudience(audience);

        //将消息保存在MongoDB
        mongoTemplate.save(ncMessage);

        //推送消息
        try {
            pushManager.sendNotificationByAlias(title, ncMessage.getContent(), ncMessage.getTargets(), null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public List<NcMessageDto> queryMessage(NcMessageQueryDTO queryDTO) {
        //组装条件
        Query query = new Query();

        if(queryDTO == null){
            queryDTO = new NcMessageQueryDTO();
        }

        if(StrUtil.isNotEmpty(queryDTO.getId())){
            query.addCriteria(Criteria.where("_id").is(queryDTO.getId())); // where id = ?
        }
        if(StrUtil.isNotEmpty(queryDTO.getType())){
            query.addCriteria(Criteria.where("messageType").is(queryDTO.getType())); // messageType = ?
        }
        if(queryDTO.getUserId() != null){
            query.addCriteria(Criteria.where("applyUserId").is(queryDTO.getUserId())); // applyUserId = ?
        }
        if(queryDTO.getTargetId()!=null){
            query.addCriteria(Criteria.where("approveUserId").is(queryDTO.getTargetId())); // approveUserId = ?
        }

        query.with(Sort.by(Sort.Direction.ASC,"approveStatue"));

        //查询
        List<NcMessage> messageList = mongoTemplate.find(query, NcMessage.class);

        //组装数据返回
        return BeanHelper.copyWithCollection(messageList, NcMessageDto.class);
    }


    @Override
    public void updateMessageWithApproved(String messageId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(messageId)));

        Update update = new Update();
        update.set("approveStatue",1);
        update.set("useStatus",1);

        mongoTemplate.updateFirst(query, update, NcMessage.class);//更新
    }
}
